import re


def test_patterns(text: str, patterns: list):
    """
    使用正则表达式 patterns 匹配字符串 text 中的内容
    :param text: 要匹配的字符串
    :param patterns: 使用的正则表达式
    :return:
    """
    for pattern, desc in patterns:
        print("'{}' ({})\n".format(pattern, desc))
        print("  '{}'".format(text))

        matches = re.finditer(pattern, text)
        for match in matches:
            s = match.start()
            e = match.end()
            substr = text[s:e]
            n_backslashes = text[:s].count('\\')
            prefix = '.' * (s + n_backslashes)
            print("  {}'{}'".format(prefix, substr))




if __name__ == '__main__':
    # 要测试的正则表达式及其解释
    patterns = [
        ("this", "匹配单词 this"),
        ("that", "匹配单词 that"),
    ]

    # 要匹配的文本内容
    text = "Does this text match the pattern？"

    test_patterns(text=text, patterns=patterns)
